package bb.lanxing.adapter.sport;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

import bb.lanxing.App;
import bb.lanxing.fragment.sport.DevicePanelSettingTypeFragment;
import bb.lanxing.model.sport.SportTypeGroup;

public class DevicePanelSettingTabAdapter extends FragmentPagerAdapter {
    private List<String> mTablayoutlist = new ArrayList<>();
    public List<DevicePanelSettingTypeFragment> mPanelSettingTypeFragments = new ArrayList<>();

    public int mSelectedTab = -1;
    public int mLastSelectedTab = -1;

    public DevicePanelSettingTabAdapter(SportTypeGroup[] CATEGORY, FragmentManager fm) {
        super(fm);
        for (int i = 0; i < CATEGORY.length; i++) {
            mTablayoutlist.add(App.getContext().getString(CATEGORY[i].getTitle()));
            DevicePanelSettingTypeFragment fragment = new DevicePanelSettingTypeFragment(i, CATEGORY[i].getTypes());
            mPanelSettingTypeFragments.add(fragment);
        }
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        return mPanelSettingTypeFragments.get(position);
    }

    @Override
    public int getCount() {
        return mPanelSettingTypeFragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mTablayoutlist.get(position);
    }

    public void updateSelectedTab(int selectedTab) {
        mLastSelectedTab = mSelectedTab;
        mSelectedTab = selectedTab;
        mPanelSettingTypeFragments.get(selectedTab).mSelectedTab = selectedTab;
    }

    public void removeUnselectedItem() {
        if (mLastSelectedTab >= 0) {
            mPanelSettingTypeFragments.get(mLastSelectedTab).getDevicePanelSettingTypeAdapter().removeUnselectedItem();
        }
    }

    public void updateSelectedItem(int selectedTab, int selectedItem) {
        mPanelSettingTypeFragments.get(mSelectedTab).getDevicePanelSettingTypeAdapter().updateSelectedItem(selectedTab,
                selectedItem);
    }
}